package com.minjiang.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.minjiang.common.result.Result;
import com.minjiang.model.system.FoodDetailType;
import com.minjiang.model.system.FoodType;
import com.minjiang.model.system.MyUser;
import com.minjiang.model.vo.FoodTypeVo;
import com.minjiang.model.vo.MyUserQueryVo;
import com.minjiang.system.mapper.sec.FoodDetailTypeMapper;
import com.minjiang.system.mapper.sec.FoodTypeMapper;
import com.minjiang.system.service.sec.FoodDetailTypeService;
import com.minjiang.system.service.sec.FoodTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "食物分类管理")
@RestController
@RequestMapping("/admin/system/foodType")
public class FoodTypeController {

    @Autowired
    private FoodTypeService foodTypeService;
    @Autowired
    private FoodDetailTypeService foodDetailTypeService;
    @Autowired
    private FoodDetailTypeMapper foodDetailTypeMapper;

    @ApiOperation(value = "获取食物分类分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "userQueryVo", value = "查询对象", required = false)
                    FoodTypeVo foodTypeVo) {
        Page<FoodType> pageParam = new Page<>(page, limit);
        IPage<FoodType> pageModel = foodTypeService.selectPage(pageParam, foodTypeVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "根据id获取食物分类")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable String id) {
        FoodType foodType = foodTypeService.getById(id);
        return Result.ok(foodType);
    }

    @ApiOperation(value = "获取全部分类")
    @GetMapping("/get/all")
    public Result getAll(){
        List<FoodType> lists = foodTypeService.selectList();
        lists.forEach(item->{
            LambdaQueryWrapper<FoodDetailType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FoodDetailType::getFoodTypeId,item.getId());
            List<FoodType> collect = foodDetailTypeMapper.selectList(queryWrapper).stream().map(res -> new FoodType(res.getId(), res.getTypeName())).collect(Collectors.toList());
            item.setChildren(collect);
        });
        return Result.ok(lists);
    }

    @ApiOperation(value = "删除分类")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable String id){
        boolean b = foodDetailTypeService.removeById(id);
        if (b){
            return Result.ok();
        }
        boolean isOk = foodTypeService.removeById(id);
        if (isOk){
            return Result.ok();
        }
        return Result.fail("删除分类失败");
    }

    @ApiOperation(value = "添加分类")
    @PostMapping("/save")
    public Result save(@RequestBody FoodType foodType){
        if (foodType.getId()==null){
            boolean save = foodTypeService.save(foodType);
            if (save){
                return Result.ok();
            }
        }else{
            FoodDetailType foodDetailType = new FoodDetailType();
            foodDetailType.setFoodTypeId(foodType.getId());
            foodDetailType.setTypeName(foodType.getTypeName());
            boolean save = foodDetailTypeService.save(foodDetailType);
            if (save){
                return Result.ok();
            }
        }

        return Result.fail("添加分类失败");
    }

    @ApiOperation(value = "更新分类")
    @PutMapping("/update")
    public Result updateById(@RequestBody FoodType foodType) {
        foodTypeService.updateById(foodType);
        return Result.ok();
    }
}
